<?php

namespace app\merchant\controller;

use app\common\model\ChangeLog;
use app\common\model\MchStatis;
use app\common\model\Mstatis;
use app\common\model\Notice;
use app\common\model\Statis;
use app\common\controller\AdminBase;
use app\common\model\Paylog;
use think\Session;


class Index extends AdminBase
{
    
    public function index()
    {
//        var_dump($_SESSION);
//        var_dump($_SESSION);
        $sarr = Session::get();
        $paylog = new Paylog();
        //当日交易统计
        $day = strtotime(date('Y-m-d'));
        $day2 = strtotime(date('Y-m-d', strtotime('+1 day')));
        $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
        $map['pay_statu'] = ['not in','0,8'];
        $mer_mid = $this->get_mer_mid();
        $map['mid'] = array('in', $mer_mid);
        $map['pay_cate'] = array('neq', 'recharge');
        $paylog5 = $paylog->where($map)->order('id desc')->limit('6')->select();
        $ordercount = $paylog->where($map)->count();
        $pointcount = $paylog->where($map)->sum('price');
        $machcount = $paylog->where($map)->count('Distinct mid');
        //END
        //本周交易统计
        if (date('w') == 1) {
            $week_start = strtotime(date('Y-m-d'));
        } else {
            $week_start = strtotime('last monday');
        }
        if (date('w') == 0) {
            $week_end = strtotime(date('Y-m-d'));
        } else {
            $week_end = strtotime("next sunday");
        }
        $map2['add_time'] = array('between', '' . $week_start . ',' . $week_end . '');
        $map2['pay_statu'] = ['not in','0,8'];
        $map2['mid'] = array('in', $mer_mid);
        $map2['pay_cate'] = array('neq', 'recharge');
        $pointweekcount = $paylog->where($map2)->sum('price');
        //END
        //本月交易统计
        $map3['mid'] = array('in', $mer_mid);
        $mon_start = strtotime(date("Y-m-01"));
        $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
        $mon_end = strtotime("$BeginDate +1 month -1 day");
        $map3['add_time'] = array('between', '' . $mon_start . ',' . $mon_end . '');
        $map3['pay_statu'] = ['not in','0,8'];
        $map3['pay_cate'] = array('neq', 'recharge');
        $pointmoncount = $paylog->where($map3)->sum('price');
        //END
        //本年度交易统计
        $year_start = strtotime(date("Y-01-01"));
        $year_end = strtotime("+1 year", $year_start) - 1;
        $map4['mid'] = array('in', $mer_mid);
        $map4['add_time'] = array('between', '' . $year_start . ',' . $year_end . '');
        $map4['pay_statu'] = ['not in','0,8'];
        $map4['pay_cate'] = array('neq', 'recharge');
        $pointyearcount = $paylog->where($map4)->sum('price');
        //END
        //月度详细交易统计
        $days = strtotime(date('Y-m-d'));
        $days2 = strtotime(date('Y-m-d', strtotime('-7 day')));
        $Statis = new MchStatis();
        $tj['mid'] = array('in', $mer_mid);
        $tj['statis_times'] = array('between', [$days2, $days]);
        $tongji = $Statis->where($tj)->order('statis_times')->select();
        //END
        //图表
        $tongji = collection($tongji)->toArray();
        if ($tongji) {
            $dates = [];
            foreach ($tongji as $k => $v) {
                $dates[] = date('Y-m-d', $v['statis_times']);
            }
            $dated=array_merge(array_unique($dates));
            $order = [];
            $price = [];
            foreach($dated as $v){
                $order[] = $Statis->where(array('mid'=>array('in', $mer_mid),'statis_times'=>strtotime($v)))->sum('real_order');
                $price[] = $Statis->where(array('mid'=>array('in', $mer_mid),'statis_times'=>strtotime($v)))->sum('real_fee');
            }
            if (isset($dates)) {
                $dated = array_merge(array_unique($dates));
            } else {
                $datad = '';
            }
            $tongji2 = array('riqi' => json_encode($dated), 'order' => json_encode($order), 'price' => json_encode($price));
            $this->assign('tongji2', $tongji2);
        } else {
            $tongji2 = array('riqi' => json_encode(''), 'order' => json_encode(''), 'price' => json_encode(''));
            $this->assign('tongji2', $tongji2);
        }
        //系统公告
        $Notice=new Notice();
        $noticelist=$Notice->where(array('level'=>0))->order('id desc')->select();
        $this->assign('notice',$noticelist);
        $this->assign('pointyearcount', number_format($pointyearcount, 2));
        $this->assign('pointmoncount', number_format($pointmoncount, 2));
        $this->assign('pointweekcount', number_format($pointweekcount, 2));
        $this->assign('machcount', $machcount);
        $this->assign('ordercount', $ordercount);
        $this->assign('pointcount', number_format($pointcount, 2));
        $this->assign('paylog5', $paylog5);
        //更新公告
        $changelist =$this->getchangelog();
        $this->assign('changelist',$changelist);
        return $this->fetch();
    }

    //更新公告输出
    public function getchangelog(){
        $ChangLog = new ChangeLog();
        $list = $ChangLog->order('id desc')->select();
        return $list;
    }

    //更新日志管理
    public function changemlog(){
        $list = $this->getchangelog();
        $this->assign('list',$list);
        return $this->fetch();
    }

    //更新日志详情
    public function changeminfo(){
        $id = request()->param('id');
        $ChangLog = new ChangeLog();
        $info = $ChangLog->where(['id'=>$id])->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
}
